from flask import Flask,render_template,request
import sqlite3

app = Flask(__name__)
datalist = []   #设为全局变量，用于存储数据和分页使用
movieScore = []  #评分
num = []    #每个评分所统计的电影数量
page=[1,2,3,4,5,6,7,8,9,10]

@app.route('/')
def index():
    return render_template('first.html')
@app.route('/first')
def first():
    return index()
@app.route('/movie')
def movie():
    if len(datalist) == 0:
        # print("关于电影数据库被启动了!")
        con = sqlite3.connect('./movieTop250.db')
        cur = con.cursor()
        sql = "select * from movie250"
        data = cur.execute(sql)
        for item in data:
            datalist.append(item)
        cur.close()
        con.close()
    page=1
    movies=[]
    #对列表进行分割
    for i in range(25):
        movies.append(datalist[i])
    return render_template('movie.html',movies=movies,page=page)
#进行分页
@app.route('/movie/start=<int:page>')
def movie2(page):
    movies=[]
    #对列表进行分割
    if page:    #page存在
        first=(page-1)*25   #首页
        last=page*25    #尾页
    else :  
        first=0   #首页
        last=25    #尾页
    for i in range(first,last):
        movies.append(datalist[i])
    #列表分割完成
    if page == 1:
        return render_template('movie.html',movies=movies,page=page)
    elif page == 2:
        return render_template('movie.html',movies=movies,page=page)
    elif page == 3:
        return render_template('movie.html',movies=movies,page=page)
    elif page == 4:
        return render_template('movie.html',movies=movies,page=page)
    elif page == 5:
        return render_template('movie.html',movies=movies,page=page)
    elif page == 6:
        return render_template('movie.html',movies=movies,page=page)
    elif page == 7:
        return render_template('movie.html',movies=movies,page=page)
    elif page == 8:
        return render_template('movie.html',movies=movies,page=page)
    elif page == 9:
        return render_template('movie.html',movies=movies,page=page)
    elif page == 10:
        return render_template('movie.html',movies=movies,page=page)
    else :
        return render_template('movie.html',movies=movies,page=page)

@app.route('/score')
def score():
    if (movieScore == [] or num == []):
        # print("关于评分数据库被启动了!")
        con = sqlite3.connect('./movieTop250.db')
        cur = con.cursor()
        sql = "select score,count(score) from movie250 group by score"
        data = cur.execute(sql)
        for item in data:
            #是元组类型
            movieScore.append(item[0])
            num.append(item[1])
        cur.close()
        con.close()
    return render_template('score.html',movieScore=movieScore,num=num)
@app.route('/word')
def word():
    return render_template('word.html')
@app.route('/team')
def team():
    return render_template('team.html')
@app.route('/echarts')
def echarts():
    return render_template('test_echarts.html')

if __name__ == "__main__":
    app.run(debug=True)